# _*_ coding:UTF-8 _*_
"""
__author__ = 'shede333'
"""

from pathlib import Path

from . import shell_command


def _print_output(command, *info_list):
    print("{}{}{}".format("*" * 15, "git", "*" * 15))
    print("command: ")
    print("\t{}".format(command))
    print("output:")
    print("\n".join(["\t" + tmp_info for tmp_info in info_list]))
    print("*" * 30)


def _run_command(command, is_check, print_log):
    run_code, run_output = shell_command.call(command, is_check)
    if print_log:
        _print_output(command, run_output)
    return run_code, run_output


def clone(url, des_dir_path, is_check=False, print_log=True):
    command = "git clone {0} --recurse {1}".format(url, des_dir_path)
    return _run_command(command, is_check, print_log)


def pull(dir_path, print_log=True):
    command = "cd {0}; git pull".format(dir_path)
    return _run_command(command, print_log=print_log)


def status(dir_path, print_log=True):
    command = "cd '{0}'; git status".format(dir_path)
    return _run_command(command, print_log=print_log)


def work_space_is_clean(dir_path):
    out_put = status(dir_path)[1]
    if "nothing to commit, working directory clean" in out_put:
        return True
    else:
        return False


def user_name(dir_path=None):
    dir_path = dir_path if dir_path else Path.home()
    command = "git config --get user.name"
    return shell_command.check_output(command, cwd=dir_path).strip()


def user_email(dir_path=None):
    dir_path = dir_path if dir_path else Path.home()
    command = "git config --get user.email"
    return shell_command.check_output(command, cwd=dir_path).strip()
